<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="服务器" prop="serviceName">
        <el-select v-model="queryParams.serviceId" placeholder="请选择">
          <el-option
            v-for="item in serviceLists"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-card class="box-card" id="slider">
      <div v-for="mes in messageList" :key="mes" class="text item"  >
        {{ mes }}
      </div>
    </el-card>
    <div>
      <el-form label-width="68px" >
        <el-form-item >
          <el-input
            style="width: 95%;padding-top: 15px"
            v-model="message"
            placeholder="发送内容"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            ref="submitBtn"
            type="primary"
            @click="sendMessage"
            style="width: 95%"
          >发送
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-select v-model="selectValue" placeholder="请选择自定义内容"  @change="changeSelect" style="width: 95%" clearable>
            <el-option
              v-for="item in customMessageSelectOption"
              :key="item.message"
              :label="item.message"
              :value="item.message"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            ref="submitBtn"
            type="warning"
            @click="editCustomContent"
            style="width: 95%"
            v-hasPermi="['send:broadcast:edit']"
          >编辑自定义内容
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 地图导入对话框 -->
    <el-dialog title="编辑自定义内容" :visible.sync="editCustomContentVisible" width="90%" append-to-body>
      <el-form :model="queryCustomParams" ref="queryForm" size="small" :inline="true"  label-width="68px" @submit.native.prevent>
        <el-form-item label="信息内容" prop="message">
          <el-input
            v-model="queryCustomParams.message"
            placeholder="请输入信息内容"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            :disabled="multiple"
            size="mini"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="customMessageList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="信息内容" align="center" prop="message" show-overflow-tooltip/>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryCustomParams.pageNum"
        :limit.sync="queryCustomParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>

    <!-- 添加或修改广播自定义消息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="信息内容" prop="message">
          <el-input v-model="form.message" placeholder="请输入信息内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import store from '@/store'
import { sendBroadcast } from '@/api/suqad/rcon'
import { listMessage, getMessage, delMessage, addMessage, updateMessage } from "@/api/suqad/customMessage";
export default {
  name: 'Broadcast',
  data() {
    return {
      selectValue:null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids:[],
      open:false,
      title: null,
      // 遮罩层
      loading: true,
      editCustomContentVisible:false,
      messageList: [],
      customMessageSelectOption: [],
      customMessageList: [],
      message: '',
      total: 0,
      queryCustomParams: {
        pageNum: 1,
        pageSize: 10,
        message: null,
        time: null
      },
      queryParams: {
        time: null,
        message: null,
        serviceId: null,
        sendName: null
      },
      form:{},
      serviceLists:[],
      // 表单校验
      rules: {
      },
    }
  },
  watch:{
    editCustomContentVisible:{
      handler(now, old) {
        if (now === true) {
          this.getList()
        }else {
          this.getCustomMessageSelectOption()
        }

      }

    }
  },
  created() {
    this.serviceLists = store.getters.serviceLists
    this.queryParams.serviceId = this.serviceLists[0].id
    this.getCustomMessageSelectOption()
  },
  methods: {
    changeSelect(){
      this.message = this.selectValue
    },
    getCustomMessageSelectOption() {
      listMessage({
        pageNum: 1,
        pageSize: 10000
      }).then(response => {
        this.customMessageSelectOption = response.rows;
      });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        message: null,
        time: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加广播自定义消息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      console.log(id)
      getMessage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改广播自定义消息";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除广播自定义消息编号为"' + ids + '"的数据项？').then(function() {
        return delMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 查询广播自定义消息列表 */
    getList() {
      this.loading = true;
      listMessage(this.queryCustomParams).then(response => {
        this.customMessageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log("qwe")
      this.queryCustomParams.pageNum = 1;
      this.getList();
    },
    editCustomContent(){
      this.editCustomContentVisible=true
    },
    sendMessage() {
      var currentDate = new Date()
      // 获取年、月、日、小时、分钟和秒
      var year = currentDate.getFullYear()
      var month = ('0' + (currentDate.getMonth() + 1)).slice(-2)
      var day = ('0' + currentDate.getDate()).slice(-2)
      var hours = ('0' + currentDate.getHours()).slice(-2)
      var minutes = ('0' + currentDate.getMinutes()).slice(-2)
      var seconds = ('0' + currentDate.getSeconds()).slice(-2)
      // 拼接成 yyyy-MM-dd hh:mm:ss 格式
      var formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
      this.messageList.push(formattedDate + ':' + store.getters.name + ':' + this.message)
      this.queryParams.message = this.message
      this.queryParams.time = formattedDate
      this.queryParams.sendName = store.getters.name
      this.message=''
      sendBroadcast(this.queryParams).then(res=>{

      })
    }
  }
}
</script>
<style>
.text {
  font-size: 14px;
}

.item {
  padding: 5px 0;
}

.box-card {
  width: 100%;
  height: 450px;
}
#slider{
  overflow: auto; /* 自动开启滚动条 */
}
</style>

